package com.yunji.order.task;

import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.yunji.base.enums.AuditStatus;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.order.bo.GetAuditApplyBO;
import com.yunji.order.business.PayoutBusiness;
import com.yunji.order.business.RefundBusiness;
import com.yunji.order.business.remote.AlibabaPayable;
import com.yunji.order.business.remote.WechatPayable;
import com.yunji.order.domain.PayoutDO;
import com.yunji.order.emuns.PayoutStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Component("tradeTask")
public class TradeTask {
    private static final Logger logs = LoggerFactory.getLogger(TradeTask.class);

    @Resource
    private PayoutBusiness payoutBusiness;

    @Resource
    private RefundBusiness refundBusiness;

    @Resource
    private AlibabaPayable alibabaPayable;

    @Resource
    private WechatPayable wechatPayable;

//    public void refund() {
//        List<RefundDO> list = refundBusiness.searchAuditRefund(new GetAuditApplyBO(AuditStatus.REJECT.getCode(),
//                TimeUtils.toDate(LocalDate.now().minusDays(1L))));
//        if (BeanUtils.isNull(list)) {
//            return;
//        }
//        for (RefundDO refund : list) {
//            logs.info("【订单退款】退款信息:{}", refund);
//            Long refundId = refund.getRefundId();
//            try {
//                wechatPayable.searchRefund(refund.getRefundCode());
//                refundBusiness.refundCallback(refund);
//                logs.info("【订单退款】退款{}处理成功", refundId);
//            } catch (Exception e) {
//                logs.error("【订单退款】退款{}处理失败", refundId, e);
//            }
//        }
//    }

    public void payout() {
        List<PayoutDO> list = payoutBusiness.searchAuditPayout(new GetAuditApplyBO(AuditStatus.REJECT.getCode(),
                TimeUtils.toDate(LocalDate.now().minusDays(1L))));
        if (BeanUtils.isNull(list)) {
            return;
        }
        for (PayoutDO payout : list) {
            logs.info("【用户转账】转账信息:{}", payout);
            Long payoutId = payout.getPayoutId();
            try {
                AlipayFundTransCommonQueryResponse rs = alibabaPayable.searchPayout(payout.getPayoutCode());
                if (rs.isSuccess()) {
                    payoutBusiness.callback(rs.getOutBizNo(), rs.getPayFundOrderId(), rs.getPayDate());
                    logs.info("【用户转账】转账{}处理成功", payoutId);
                } else {
                    PayoutDO payoutDO = new PayoutDO();
                    payoutDO.setPayoutId(payoutId);
                    payoutDO.setApplyStatus(PayoutStatusEnum.FAILURE.getCode());
                    payoutBusiness.updatePayout(payoutDO);
                }
            } catch (Exception e) {
                logs.error("【用户转账】转账{}处理失败", payoutId, e);
            }
        }
    }
}